/***************************************************************************
* HEADER-FILES (Only those that are needed in this file)
****************************************************************************/
/* System-headerfiles */
#include <stdio.h>      /* ���ڵ������ */
#include <stdbool.h>    /* �������� */
#include <stdint.h>     /* ��׼�������� */

/* Own headerfiles */
#include "gear1c1.h"    /* ģ����ͷ�ļ� */
#include "../adapt/gear1ci.h" /* �ӿڶ��� */
#include "../adapt/gear1ct.h" /* ���Ͷ��� */

/***************************************************************************
* GLOBAL DEFINITIONS
*
* In this section define
* - all global ROM-constants of your module
* - all global variables of your module
****************************************************************************/
// ���ݳر������壨��gear1ci.hһ�£�
DPOOL_txGEAR_tstGearData DPOOL_txGEAR_stGearData = {
    .GEAR_boGearValid = false,   // ��ʼ��λ��Ч
    .GEAR_boGearFlashSts = false, // ��ʼ����˸
    .GEAR_u8GearPosi = GEAR_nGearBlank  // ��ʼ�յ�λ
};

DPOOL_txGEAR_tstShiftSymbolData DPOOL_txGEAR_stShiftSymbolData = {
    .GEAR_u8ShiftSymbolPosi = GEAR__nenSymbolBlank  // ��ʼ�޻�������
};

// ����״̬������������˸���ƣ�
static uint8_t gu8FlashCounter = 0;  // ��˸��������0-9��100ms���ڣ�
static bool gbFlashState = false;   // ��ǰ��˸״̬��true=��ʾ��false=���أ�

/***************************************************************************
* LOCAL FUNCTION PROTOTYPES
*
* �������ظ������������ڱ��ļ���ʹ�ã�
****************************************************************************/
static GEAR_tenGearState GEAR_enConvertToDisplayGear(uint8_t u8DrvMod, uint8_t u8CurGear, bool bFlashing);
static GEAR_tenSymbolType GEAR_enGetShiftSymbol(uint8_t u8ShiftRec, uint8_t u8UpShift, uint8_t u8DownShift);
static void GEAR_vUpdateFlashState(void);
static bool GEAR_bIsTransmissionValid(void);

/***************************************************************************
* FUNCTION DEFINITIONS
****************************************************************************/

/**
 * @brief ģ����������ÿ100ms����һ�θ��µ�λ����ʾ
 */
void GEAR_vMain(void) {
    SYN_tstGearDpoolData stGearPosi;
    SYN_tstShiftSymbolDpoolData stShiftSymbol;

    SYN_enGetDpoolGearData(&stGearPosi); //从dpool中获取gear挡位数据：GEAR_nGearBlank，GEAR_nGearP...
    SYN_enGetDpoolShiftSymbolData(&stShiftSymbol); //从dpool中获取换挡提示的数据：GEAR__nenSymbolBlank，GEAR__nenSymbolUp，GEAR__nenSymbolDown

    //DrvModDisp_read(); //CAN信号接口
    //DisplayGear_read(); //CAN信号接口
    //TCU_gear_flashing_read(); //CAN信号接口
    //ShiftRecommend_M_read(); //CAN信号接口
    //Tgtgear_Up_read(); //CAN信号接口
    //Tgtgear_Down_read(); //CAN信号接口
    //EEH_biGetCFG2_Transmission(); //下线配置：变速器:GEAR__nu8Transmission8AT, GEAR__nu8Transmission6MT


	/***************************************************************************
	* FUNCTION CODE
	****************************************************************************/

    uint8 shiftRecommend = ShiftRecommend_M_read(); // 获取换挡建议
    // 处理换挡提示

    switch (shiftRecommend) {
    case 0x1: // 向上换挡提示
        stShiftSymbol.GEAR_u8ShiftSymbolPosi = GEAR__nenSymbolUp;
        break;

    case 0x2: // 向下换挡提示
        stShiftSymbol.GEAR_u8ShiftSymbolPosi = GEAR__nenSymbolDown;
        break;

    default: // 其他情况
        stShiftSymbol.GEAR_u8ShiftSymbolPosi = GEAR__nenSymbolBlank;
        break;
    }

    uint8 tgtgearUp = Tgtgear_Up_read();
    uint8 tgtgearDown = Tgtgear_Down_read();

    if (tgtgearUp == 1 && tgtgearDown == 0) {
        stShiftSymbol.GEAR_u8ShiftSymbolPosi = GEAR__nenSymbolUp;
    }
    else if (tgtgearUp == 0 && tgtgearDown == 1) {
        stShiftSymbol.GEAR_u8ShiftSymbolPosi = GEAR__nenSymbolDown;
    }
    else if (tgtgearUp == 1 && tgtgearDown == 1) {
        stShiftSymbol.GEAR_u8ShiftSymbolPosi = GEAR__nenSymbolBlank;
    }
    else if (tgtgearUp == 0 && tgtgearDown == 0) {
        stShiftSymbol.GEAR_u8ShiftSymbolPosi = GEAR__nenSymbolBlank;
    }


    // 从CAN信号获取当前的挡位和闪烁状态
    uint8 drvModDisp = DrvModDisp_read();
    uint8 displayGear = DisplayGear_read();
    uint8 flashingSignal = TCU_gear_flashing_read();

    // 处理正常状态下挡位显示
    if (drvModDisp == 0x0 && displayGear == 0xD) {
        if (flashingSignal == 0x0) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearP;
        }
        else if (flashingSignal == 0x1) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearFlashingP;
        }
        else {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
        }
    }

    if (drvModDisp == 0x3 && displayGear == 0x1) {
        if (flashingSignal == 0x0) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearD1;
        }
        else if (flashingSignal == 0x1) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearFlashingD1;
        }
        else {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
        }
    }

    if (drvModDisp == 0x3 && displayGear == 0x2) {
        if (flashingSignal == 0x0) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearD2;
        }
        else if (flashingSignal == 0x1) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearFlashingD2;
        }
        else {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
        }
    }

    if (drvModDisp == 0x3 && displayGear == 0x3) {
        if (flashingSignal == 0x0) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearD3;
        }
        else if (flashingSignal == 0x1) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearFlashingD3;
        }
        else {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
        }
    }

    if (drvModDisp == 0x3 && displayGear == 0x4) {
        if (flashingSignal == 0x0) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearD4;
        }
        else if (flashingSignal == 0x1) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearFlashingD4;
        }
        else {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
        }
    }

    if (drvModDisp == 0x3 && displayGear == 0x5) {
        if (flashingSignal == 0x0) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearD5;
        }
        else if (flashingSignal == 0x1) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearFlashingD5;
        }
        else {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
        }
    }

    if (drvModDisp == 0x3 && displayGear == 0x6) {
        if (flashingSignal == 0x0) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearD6;
        }
        else if (flashingSignal == 0x1) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearFlashingD6;
        }
        else {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
        }
    }

    if (drvModDisp == 0x3 && displayGear == 0x7) {
        if (flashingSignal == 0x0) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearD7;
        }
        else if (flashingSignal == 0x1) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearFlashingD7;
        }
        else {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
        }
    }

    if (drvModDisp == 0x3 && displayGear == 0x8) {
        if (flashingSignal == 0x0) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearD8;
        }
        else if (flashingSignal == 0x1) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearFlashingD8;
        }
        else {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
        }
    }

    if (drvModDisp == 0x2 && displayGear == 0x0) {
        if (flashingSignal == 0x0) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearN;
        }
        else if (flashingSignal == 0x1) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearFlashingN;
        }
        else {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
        }
    }

    if (drvModDisp == 0x1 && displayGear == 0xB) {
        if (flashingSignal == 0x0) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearR;
        }
        else if (flashingSignal == 0x1) {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearFlashingR;
        }
        else {
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
        }
    }

    if (drvModDisp == 0x4) {
        switch (displayGear) {
        case 0x1:
            stGearPosi.GEAR_u8GearPosi = GEAR_nGear1;
            break;

        case 0x2:
            stGearPosi.GEAR_u8GearPosi = GEAR_nGear2;
            break;

        case 0x3:
            stGearPosi.GEAR_u8GearPosi = GEAR_nGear3;
            break;

        case 0x4:
            stGearPosi.GEAR_u8GearPosi = GEAR_nGear4;
            break;

        case 0x5:
            stGearPosi.GEAR_u8GearPosi = GEAR_nGear5;
            break;

        case 0x6:
            stGearPosi.GEAR_u8GearPosi = GEAR_nGear6;
            break;

        case 0x7:
            stGearPosi.GEAR_u8GearPosi = GEAR_nGear7;
            break;

        case 0x8:
            stGearPosi.GEAR_u8GearPosi = GEAR_nGear8;
            break;
        default: // 其他情况
            stGearPosi.GEAR_u8GearPosi = GEAR_nGearBlank;
            break;
        }


    }




    SYN_enSetDpoolGearData(&stGearPosi);
    SYN_enSetDpoolShiftSymbolData(&stShiftSymbol);
}

